package dissertation.extensions.generate.resource.control;

import com.intellij.codeInsight.CodeInsightActionHandler;
import com.intellij.codeInsight.generation.OverrideImplementUtil;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiClass;
import com.intellij.psi.PsiField;
import com.intellij.psi.PsiFile;
import dissertation.common.utils.WriteAction;
import dissertation.extensions.generate.resource.view.CreateResourceDialogWrapper;
import dissertation.services.ejbclass.EJBClassService;
import dissertation.services.ejbfield.EJBFieldService;
import dissertation.services.ejbclass.IEJBClassService;
import dissertation.services.ejbfield.IEJBFieldService;
import org.jetbrains.annotations.NotNull;

import static dissertation.common.constants.Constants.*;

public class GenerateResourceCodeInsightActionHandler implements CodeInsightActionHandler {
    private IEJBClassService ejbClassService;
    private IEJBFieldService ejbFieldService;

    public GenerateResourceCodeInsightActionHandler() {
        ejbClassService = new EJBClassService();
        ejbFieldService = new EJBFieldService();
    }

    @Override
    public void invoke(@NotNull Project project, @NotNull Editor editor, @NotNull PsiFile psiFile) {
        CreateResourceDialogWrapper createResourceDialogWrapper = new CreateResourceDialogWrapper(project);
        createResourceDialogWrapper.show();
        if (createResourceDialogWrapper.isOK()) {
            String resourceToInclude = createResourceDialogWrapper.getInput();
            final String resourceImport = resourceToInclude.lastIndexOf(".") == -1 ? null : resourceToInclude.substring(0, resourceToInclude.lastIndexOf("."));
            String resourceType = resourceToInclude.lastIndexOf(".") == -1 ? resourceToInclude : resourceToInclude.substring(resourceToInclude.lastIndexOf(".") + 1);
            String resourceName = resourceType.substring(0, 1).toLowerCase() + resourceType.substring(1);

            final PsiClass aClass = OverrideImplementUtil.getContextClass(project, editor, psiFile, false);
            if (aClass != null) {
                final PsiField psiField = ejbFieldService.createFieldFromText(project, "private " + resourceType + " " + resourceName + ";");
                ejbFieldService.annotateEJBField(psiField, RESOURCE_ANNOTATION_NAME);

                Runnable cmd = new Runnable() {
                    @Override
                    public void run() {
                        if (resourceImport != null) {
                            ejbClassService.addImportStatement(aClass, resourceImport);
                        }
                        ejbClassService.addImportStatement(aClass, ANNOTATION_PACKAGE);
                        aClass.add(psiField);
                    }
                };
                CommandProcessor.getInstance().executeCommand(project, new WriteAction(cmd), null, null);
            }
        }
    }

    @Override
    public boolean startInWriteAction() {
        return true;
    }
}
